package com.xuecheng.content.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xuecheng.base.exception.XueChengPlusException;
import com.xuecheng.content.mapper.*;
import com.xuecheng.content.model.dto.BindTeachplanMediaDto;
import com.xuecheng.content.model.dto.SaveTeachplanDto;
import com.xuecheng.content.model.dto.TeachplanDto;
import com.xuecheng.content.model.po.*;
import com.xuecheng.content.service.TeachPlanService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName TeachPlanController
 * @Description 课程计划相关的接口
 * @Author kiku
 * @Date 2024/9/13 21:40
 * @Version 1.0
 */
@Api(value = "课程计划编辑接口", tags = "课程计划编辑接口")
@RestController
public class TeachPlanController {
    @Autowired
    private TeachPlanService teachPlanService;
    @Autowired
    private TeachplanMapper teachplanMapper;
    @Autowired
    private TeachplanMediaMapper teachplanMediaMapper;
    @Autowired
    private CourseTeacherMapper courseTeacherMapper;
    @Autowired
    private CourseMarketMapper courseMarketMapper;
    @Autowired
    private CourseBaseMapper courseBaseMapper;

    /**
     * 查询课程计划
     */
    @ApiOperation("查询课程计划树形结构")
    @GetMapping("/teachplan/{courseId}/tree-nodes")
    public List<TeachplanDto> getTreeNodes(@PathVariable Long courseId) {
        return teachPlanService.findTeachPlanTree(courseId);
    }

    @ApiOperation("课程计划创建或修改")
    @PostMapping("/teachplan")
    public void saveTeachplan(@RequestBody SaveTeachplanDto saveTeachplanDto) {
        teachPlanService.saveTeachPlan(saveTeachplanDto);
    }

    @ApiOperation("课程计划的删除")
    @DeleteMapping("/teachplan/{teachplanId}")
    public void deleteTeachplan(@PathVariable Long teachplanId) {
        Teachplan teachplan = teachplanMapper.selectById(teachplanId);
        Long courseId = teachplan.getCourseId();
        Long parentid = teachplan.getParentid();
        if (parentid == 0) {
            LambdaQueryWrapper<Teachplan> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Teachplan::getParentid, teachplanId);
            queryWrapper.eq(Teachplan::getCourseId, courseId);
            Integer count = teachplanMapper.selectCount(queryWrapper);
            if (count != 0) {
                XueChengPlusException.cast("课程计划信息还有子级信息，无法操作");
            }
        } else {
            LambdaQueryWrapper<TeachplanMedia> teachplanMediaLambdaQueryWrapper = new LambdaQueryWrapper<>();
            teachplanMediaLambdaQueryWrapper.eq(TeachplanMedia::getTeachplanId, teachplanId);
            teachplanMediaMapper.delete(teachplanMediaLambdaQueryWrapper);
        }
        teachplanMapper.deleteById(teachplanId);

    }

    @ApiOperation("课程向上移动")
    @PostMapping("/teachplan/moveup/{teachPlanId}")
    public void moveUp(@PathVariable Long teachPlanId) {
        Teachplan teachplan = teachplanMapper.selectById(teachPlanId);
        Long courseId = teachplan.getCourseId();
        Long parentid = teachplan.getParentid();
        LambdaQueryWrapper<Teachplan> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Teachplan::getParentid, parentid);
        queryWrapper.eq(Teachplan::getCourseId, courseId);
        queryWrapper.lt(Teachplan::getOrderby, teachplan.getOrderby());
        List<Teachplan> teachplans = teachplanMapper.selectList(queryWrapper);
        if (teachplans != null) {
            Teachplan last = teachplans.get(teachplans.size() - 1);
            Integer temp = 0;
            temp = last.getOrderby();
            last.setOrderby(teachplan.getOrderby());
            teachplan.setOrderby(temp);
            teachplanMapper.updateById(last);
            teachplanMapper.updateById(teachplan);
        }
    }

    @ApiOperation("课程向下移动")
    @PostMapping("/teachplan/movedown/{teachPlanId}")
    public void moveDown(@PathVariable Long teachPlanId) {
        Teachplan teachplan = teachplanMapper.selectById(teachPlanId);
        Long courseId = teachplan.getCourseId();
        Long parentid = teachplan.getParentid();
        LambdaQueryWrapper<Teachplan> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Teachplan::getParentid, parentid);
        queryWrapper.eq(Teachplan::getCourseId, courseId);
        queryWrapper.gt(Teachplan::getOrderby, teachplan.getOrderby());
        List<Teachplan> teachplans = teachplanMapper.selectList(queryWrapper);
        if (teachplans != null) {
            Teachplan last = teachplans.get(0);
            Integer temp = 0;
            temp = last.getOrderby();
            last.setOrderby(teachplan.getOrderby());
            teachplan.setOrderby(temp);
            teachplanMapper.updateById(last);
            teachplanMapper.updateById(teachplan);
        }
    }

    @ApiOperation("查询师资信息")
    @GetMapping("/courseTeacher/list/{courseId}")
    public List<CourseTeacher> getCourseTeacherInfo(@PathVariable Long courseId) {
        List<CourseTeacher> courseTeachers = courseTeacherMapper.selectList(Wrappers.<CourseTeacher>lambdaQuery().eq(CourseTeacher::getCourseId, courseId));
        if (courseTeachers == null) {
            XueChengPlusException.cast("教师不存在");
        }
        return courseTeachers;
    }

    @ApiOperation("添加师资信息")
    @PostMapping("/courseTeacher")
    public CourseTeacher addCourseTeacherInfo(@RequestBody CourseTeacher courseTeacher) {
        if (courseTeacher.getId() != null) {
            courseTeacherMapper.updateById(courseTeacher);
            return courseTeacher;
        }
        courseTeacherMapper.insert(courseTeacher);
        return courseTeacher;
    }

    @ApiOperation("修改师资信息")
    @PutMapping("/courseTeacher")
    public CourseTeacher updateCourseTeacherInfo(@RequestBody CourseTeacher courseTeacher) {
        courseTeacherMapper.updateById(courseTeacher);
        return courseTeacher;
    }

    @ApiOperation("删除师资信息")
    @DeleteMapping("/courseTeacher/course/{courseId}/{id}")
    public void deleteCourseTeacherInfo(@PathVariable Long courseId, @PathVariable Long id) {
        courseTeacherMapper.delete(Wrappers.<CourseTeacher>lambdaQuery().eq(CourseTeacher::getCourseId, courseId).eq(CourseTeacher::getId, id));

    }

    @ApiOperation("删除课程")
    @DeleteMapping("/course/{courseId}")
    @Transactional
    public void deleteCourse(@PathVariable Long courseId) {
        int delete = courseBaseMapper.delete(Wrappers.<CourseBase>lambdaQuery().eq(CourseBase::getId, courseId).eq(CourseBase::getAuditStatus, "202002"));
        if (delete==0){
            XueChengPlusException.cast("课程删除失败");
        }
        courseMarketMapper.delete(Wrappers.<CourseMarket>lambdaQuery().eq(CourseMarket::getId,courseId));
        teachplanMapper.delete(Wrappers.<Teachplan>lambdaQuery().eq(Teachplan::getCourseId,courseId));
        teachplanMediaMapper.delete(Wrappers.<TeachplanMedia>lambdaQuery().eq(TeachplanMedia::getCourseId,courseId));
        courseTeacherMapper.delete(Wrappers.<CourseTeacher>lambdaQuery().eq(CourseTeacher::getCourseId, courseId));

    }

    @ApiOperation(value = "课程计划和媒资信息绑定")
    @PostMapping("/teachplan/association/media")
    public void associationMedia(@RequestBody BindTeachplanMediaDto bindTeachplanMediaDto){
        teachPlanService.associationMedia(bindTeachplanMediaDto);
    }


}
